class User {
    constructor(username, password) {
        this.username = username;
        this.password = password;
    }
}

//注册用户
Mock.mock("/js/addUser", "post", function(obj) {
    let bodyObj = JSON.parse(obj.body);
    //获取本地存储
    let userString = localStorage.User;

    //判断本地存储是否存在
    if (userString == "" || userString == null || userString == undefined || userString == []) {
        let user = new User(bodyObj.username, bodyObj.password);
        let arr = [user];
        localStorage.User = JSON.stringify(arr);
        return true;
    } else {
        let bool = false;
        let userObj = JSON.parse(userString);
        for (let i = 0; i < userObj.length; i++) {
            if (userObj[i].username == bodyObj.username) {
                bool = true;
                break;
            }
        }

        if (bool) {
            return false;
        } else {
            let user = new User(bodyObj.username, bodyObj.password);
            userObj.push(user);
            localStorage.User = JSON.stringify(userObj);
            return true;
        }
    }

})

//登录
Mock.mock("/js/login", "post", function(obj) {
    let bodyObj = JSON.parse(obj.body);
    //获取本地存储
    let userString = localStorage.User;
    let userArr = JSON.parse(userString);
    let bool = false;
    for (let i = 0; i < userArr.length; i++) {
        if (userArr[i].username == bodyObj.username && userArr[i].password == bodyObj.password) {
            bool = true;
            break
        }
    }
    return bool;
})